package cn.bluemobi.platform.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluemobi.common.core.dto.MapDto;
import cn.bluemobi.common.core.dto.Page;
import cn.bluemobi.common.core.dto.PageCondition;
import cn.bluemobi.platform.core.mybatis.PageUtils;
import cn.bluemobi.platform.entity.category.CommodityCategory;
import cn.bluemobi.platform.entity.system.User;
import cn.bluemobi.platform.mapper.BannerMapper;
import cn.bluemobi.platform.mapper.CommodityCategoryMapper;
import cn.bluemobi.platform.service.CatagoryService;

@Service
@Transactional
public class CatagoryServiceImpl implements CatagoryService {

	@Autowired
	private CommodityCategoryMapper categoryMapper;

	@Autowired
	private BannerMapper bannerMapper;

	@Override
	public Page<Map<String, Object>> findCatagoryList(PageCondition cond) {
		Page<Map<String, Object>> page = PageUtils.startPage(cond);
		page.setList(categoryMapper.findPages(cond.getMap()));
		return page;
	}

	@Override
	public CommodityCategory findCategoryDetail(String catagoryId) {
		CommodityCategory catagory = categoryMapper.selectByPrimaryKey(Long
				.valueOf(catagoryId));
		if (catagory != null) {
		}
		return catagory;
	}

	@Override
	public MapDto saveOrUpdate(CommodityCategory catagory, User user) {
		MapDto dto = new MapDto();
		if (catagory.getId() == null) {
			catagory.setIsDelete(0);
			catagory.setCreateDate(new Date());
			categoryMapper.insertSelective(catagory);

		} else {
			categoryMapper.updateByPrimaryKeySelective(catagory);
		}
		return dto;
	}

	@Override
	public MapDto deleteCatagory(Long catagoryId, User user) {
		MapDto dto = new MapDto();
		List<CommodityCategory> cc = new ArrayList<CommodityCategory>();
		cc = categoryMapper.findCatagoryByUsed(catagoryId);
		if (cc != null && cc.size() > 0) {
			dto.setMsg("删除失败，该一级分类下存在二级分类！");
			dto.setStatus(1);
			return dto;
		}
		if (categoryMapper.checkIsCommodityByCatagoryId(catagoryId) > 0) {
			dto.setMsg("删除失败，该分类下存在商品！");
			dto.setStatus(1);
			return dto;
		}
		categoryMapper.deleteByPrimaryKey(Long.valueOf(catagoryId));
		dto.setStatus(0);
		return dto;
	}

	@Override
	public Page<Map<String, Object>> findSecondCatagoryList(PageCondition cond) {
		Page<Map<String, Object>> page = PageUtils.startPage(cond);
		page.setList(categoryMapper.findSecondPages(cond.getMap()));
		return page;
	}

	@Override
	public List<Map<String, Object>> findAllFirstCategory() {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		result = categoryMapper.findAllFirstCataPages();
		Map<String, Object> options = new HashMap<String, Object>();
		options.put("id", null);
		options.put("categoryName", "--请选择--");
		result.add(0, options);
		return result;
	}

	@Override
	public List<CommodityCategory> querySecondCatagoryForAjax() {
		List<CommodityCategory> result = new ArrayList<CommodityCategory>();
		result = categoryMapper.querySecondPages();
		return result;
	}

}
